#include "Type_Define.h"
#include <math.h>

uint8_t MasterCommand;
uint8_t MasterCycleTime=10;
uint8_t MinCycleTime=5;
uint8_t M_squenceCapability=0x11;//ODLen=2 PDInLen=2 PDOutLen=0;
uint8_t RevisionID=0x01;
uint8_t ProcessDataIn=2;
uint8_t ProcessDataOut=0;
uint8_t VendorID1=1;
uint8_t VendorID2=2;
uint8_t DeviceID1=3;
uint8_t DeviceID2=4;
uint8_t DeviceID3=5;
uint8_t FunctionID1;
uint8_t FuncitonID2;
uint8_t Reserved;
uint8_t System_Command;

 
 
 



 //不实现PAGE2 而实现PAGE1
uint8_t * Page_Space[ ]={
 &MasterCommand,
&MasterCycleTime,
&MinCycleTime,
&M_squenceCapability,
&RevisionID,
&ProcessDataIn,
&ProcessDataOut,
&VendorID1,
&VendorID2,
&DeviceID1,
&DeviceID2,
&DeviceID3,
&FunctionID1,
&FuncitonID2,
&Reserved,
&System_Command 

};

//Page1的读写特性
RW_TYPE Page_RW_Type[16]={
    W,
    RW,
    R,
    R,
    RW,
    R,
    R,
    R,
    R,
    RW,
    RW,
    RW,
    R,
    R,
    R,
    W,
  //待更新  E,
	  //E,E,E,E,E,E,E, E,E,E,E,E,E,E,E //Page2 未实现
    
};



//根据表B6，解析ProcessDataIn 得到PDIn长度
uint8_t ParaseTableB6(uint8_t ProcessDataIn){
	     uint8_t BYTE=ProcessDataIn&0x80;
	     uint8_t length=ProcessDataIn&0x1F;
	    if(BYTE==0x00){
				   switch(length){
						 case 0: return 0;
						 case 1:return 1; //1bit
						 case 16: return 2; //16bit ,2Byte
						 default:{
							     if(length>=2&&length<=15){
										   return (uint8_t )ceil(length/8);
									 }else{
										   return 0;
									 }
							 
						 } //default
						 
					 }  //swtich
				
				
			}else{
				   switch(length){
						 case 2 : return 3;
						 case 31:return 32;
						 default:{
							   if(length>=3&&length<=30){
									   return length+1;
								 }else{
									   return 0;
								 }
							 
						 }//default
						 
					 }//switch
				
				
				
			}
	
}


//根据B.1.3解析MasterCycle,以us为单位返回mastercycle,此函数在MD_Init中调用
uint16_t ParaseMasterCycleTime(void){
	uint8_t timebase=(MasterCycleTime&0xC0)>>6;
    uint8_t Multiper=MasterCycleTime&0x3F;
    	
    switch(timebase){
		case 0x00:return 100*Multiper;  //
		case 0x01:return 6400+400*Multiper;
		case 0x11:return 32000+1600*Multiper;
		default:return 32000+1600*Multiper;
	    
   }
	
}

